package ;
import flixel.FlxState;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.text.FlxText;

/**
 * ...
 * @author Pablo Abratte
 * @author Federico Donnet
 */
class PlayState extends FlxState
{

	//--------------------------------------------------------------------------------//
	//---*Atributos:
	//--------------------------------------------------------------------------------//
	//---*level encargado de los tileMaps:
	private	var	level:Level						=	null;
	//---*Player:
	public	var	player:Pucky					=	null;
	private static inline var VELOCITY 			= 	600;	/// velocidad del movimiento (pixeles x segundo)
	private static inline var CAMERA_SIZE_X 	= 	320;
	//---*Algunas cosas por momento auxiliares:
	//---*Texto que nos indica el numero de patron actual en pantalla:
	public	var	currentPatronText:FlxText		=	null;
	private var scoreText: FlxText 				= 	null;
	// sprite invisible que sirve como target para la camara	
	private var cameraSprite: FlxSprite 		= 	null; 
	private var isPlaying: Bool 				= 	false;
	//--------------------------------------------------------------------------------//
	//---*Metodos:
	//--------------------------------------------------------------------------------//
	override public function create():Void
	{
		super.create();
		//---*Iniciamos:
		Init(Utils.LEVEL_01_NAME);
	}
	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy(): Void
	{
		super.destroy();
	}
	
	//---*Callback para iniciar y preparar lo necesario para correr este FlxState:
	function	Init(LevelName:String):Void
	{		
		level								=	new Level();
		player								=	new Pucky(CAMERA_SIZE_X/2, 100);	
		scoreText							=	new FlxText(0, 0, 200);
		cameraSprite						=	new FlxSprite(CAMERA_SIZE_X / 2, 200);		
		level.cameraSprite					=	this.cameraSprite;
		cameraSprite.visible 				=	false;
		//---*Iniciamos el Level:
		level.Init(LevelName);			
		//---*Indicamos a la camera que el target es nuestro player:
		FlxG.camera.follow(this.cameraSprite);		
		//---*Aniadimos al flxState:
		add(level);
		add(level.itemList);
		add(player);
		add(cameraSprite);
		add(scoreText);

	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update(): Void
	{
		
		super.update();	
		switch(isPlaying)
		{
			case false:
				if (FlxG.mouse.justPressed && this.player.active)
				{
					Start();
				}
			case true:
				//---*Chequea si el player sale del view de la camera (por lo gral a la izq)
				if (!this.player.isOnScreen(FlxG.camera))
				{			
					Stop();
					FlxG.camera.shake(0.012, 0.35, onShakeComplete);					
				}				
				//---*Si llega a cierto punto (la mitad del 3er patron), cambia los patrones:
				//---*NOTA: cameraSprite tiene un ancho de 0...
				if (this.cameraSprite.x + this.cameraSprite.width >= 800)
				{
					level.Change();
					//---*Mueve el cameraSprite "2 patrones atras":
					this.cameraSprite.x = (this.cameraSprite.x - (320 * 2)) ;
					//---*Mueve el player  "2 patrones atras":
					this.player.x = this.player.x - (320 * 2);
				}				
		}
		
		//---*Colisiones:
		if (FlxG.collide(this.player, this.level) && this.isPlaying)
		{
			this.player.velocity.x 		=	VELOCITY;
		}
	}
	
	//---*Comienza el juego:
	function	Start():Void
	{		
		isPlaying 						=	true;
		this.player.velocity.x 			=	VELOCITY;
		this.cameraSprite.velocity.x 	=	VELOCITY;
	}

	//---*Detiene el Juego:
	function	Stop():Void
	{
		this.player.active				=	false;
		this.isPlaying					=	false;
		this.player.velocity.x 			=	0;
		this.cameraSprite.velocity.x	=	0;
	}
	
	//---*Callback que se llama cuando se termina de emitir el shake de la camera:
	function onShakeComplete()
	{
		this.player.active				=	true;
		level.Change();		
		this.cameraSprite.x 			=	(CAMERA_SIZE_X / 2);
		this.player.x 					= 	this.cameraSprite.x;
		this.player.y					=	100;
		FlxG.camera.update();
	}
}